package org.jeecg.chase.common.base.data;

import org.apache.commons.lang.StringUtils;
import org.jeecg.chase.common.dataaccess.data.PageRequest;
import org.jeecg.chase.common.utils.CamelUtils;
import org.jeecg.chase.common.utils.InvokeSupportUtils;

import java.util.Map;

/**
 * @author leikai
 * @version 1.0
 * @description:
 * @date 2021/07/26/0026 15:10
 */
public abstract class BaseJavaQuery extends BaseQuery implements BaseData {
    /**
     * 第几页
     */
    private Integer pageNo = 1;

    /**
     * 每页行数
     */
    private Integer pageSize = 10;

    /**
     * 排序字段
     */
    private String sortName = "";

    /**
     * 排序
     */
    private String sortOrder = "";

    /**
     * 排序
     */
    private String orderBy = "";


    public abstract String getQuerySql();

    @Override
    public String getSql() {
        String sql = getQuerySql();
        return sql;
    }

    @Override
    public String getModelId() {
        return this.getClass().getName();
    }

    @Override
    public PageRequest getPageRequest() {
        PageRequest pageRequest = new PageRequest();
        pageRequest.setPageSize(pageSize);
        pageRequest.setPageNo(pageNo);
        if (StringUtils.isNotBlank(orderBy)) {
            orderBy = CamelUtils.camelToUnderline(orderBy);
            pageRequest.setOrderBy(orderBy);
        } else {
            if (sortName == null) {
                sortName = "";
            }
            if (sortOrder == null) {
                sortOrder = "";
            }
            if (StringUtils.isNotBlank(sortName)) {
                sortName = CamelUtils.camelToUnderline(sortName);
            }
            pageRequest.setOrderBy(sortName + " " + sortOrder);
        }
        return pageRequest;
    }

    @Override
    public Object getValue(String fieldName) {
        return InvokeSupportUtils.getValue(this, fieldName);
    }

    @Override
    public void setValue(String fieldName, Object value) {
        InvokeSupportUtils.setValue(this, fieldName, value);
    }

    @Override
    public String getString(String fieldName) {
        Object value = this.getValue(fieldName);
        if (null == value) {
            return null;
        } else {
            return String.valueOf(value);
        }
    }

    @Override
    public Map<String, Object> getValueMap() {
        return InvokeSupportUtils.getValueMap(this);
    }

    public Integer getPageNo() {
        return pageNo;
    }

    public void setPageNo(Integer pageNo) {
        this.pageNo = pageNo;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public String getSortName() {
        return sortName;
    }

    public void setSortName(String sortName) {
        this.sortName = sortName;
    }

    public String getSortOrder() {
        return sortOrder;
    }

    public void setSortOrder(String sortOrder) {
        this.sortOrder = sortOrder;
    }

    public String getOrderBy() {
        return orderBy;
    }

    public void setOrderBy(String orderBy) {
        this.orderBy = orderBy;
    }
}
